package com.sscl.wifilibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

import com.sscl.wifilibrary.bean.WifiDevice;
import com.sscl.wifilibrary.intefaces.OnWifiScanStateChangedListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jackie
 */
public class WifiScanDataAndStatusBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = WifiScanDataAndStatusBroadcastReceiver.class.getSimpleName();

    /*---------------------------静态常量---------------------------*/
    private WeakReference<WifiScanner> wifiScannerWeakReference;

    /*---------------------------成员变量---------------------------*/
    /**
     * 获取到WiFi扫描的结果时的回调
     */
    private OnWifiScanStateChangedListener wifiScanResultObtainedListener;

    public WifiScanDataAndStatusBroadcastReceiver(WifiScanner wifiScannerWeakReference) {
        this.wifiScannerWeakReference = new WeakReference<>(wifiScannerWeakReference);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (context == null) {
            return;
        }
        String action = intent.getAction();
        if (action == null) {
            return;
        }

        //noinspection SwitchStatementWithTooFewBranches
        switch (action) {
            // wifi已成功扫描到可用wifi。
            case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:

                List<ScanResult> scanResults = com.sscl.wifilibrary.WifiManager.getSystemWifiManager().getScanResults();
                int size = scanResults.size();
                // 每次最大元素就像气泡一样"浮"到数组的最后
                for (int j = 0; j < size - 1; j++) {
                    // 依次比较相邻的两个元素,使较小的那个向后移
                    for (int i = 0; i < size - 1 - j; i++) {
                        ScanResult curScanResult = scanResults.get(i);
                        ScanResult nextScanResult = scanResults.get(i + 1);
                        if (curScanResult.level < nextScanResult.level) {
                            scanResults.set(i, nextScanResult);
                            scanResults.set(i + 1, curScanResult);
                        }
                    }
                }

                for (int i = 0; i < size; i++) {
                    ScanResult scanResult = scanResults.get(i);
                    String ssid = scanResult.SSID;
                    if (ssid == null || "".equals(ssid)) {
                        scanResults.remove(i);
                        scanResults.add(scanResult);
                        break;
                    }
                }

                ArrayList<WifiDevice> wifiDevices = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    WifiDevice wifiDevice;
                    ScanResult scanResult = scanResults.get(i);
                    String ssid = scanResult.SSID;
                    if (ssid == null || "".equals(ssid)) {
                        scanResult.SSID = context.getString(R.string.com_sscl_wifilibrary_hidden_network);
                        wifiDevice = new WifiDevice(scanResult, true);
                    } else {
                        wifiDevice = new WifiDevice(scanResult);

                    }
                    wifiDevices.add(wifiDevice);
                }
                WifiScanner wifiScanner = wifiScannerWeakReference.get();
                if (wifiScanner == null) {
                    return;
                }
                if (wifiScanner.isScanning()) {
                    if (wifiScanResultObtainedListener != null) {
                        wifiScanResultObtainedListener.wifiScanResultObtained(wifiDevices);
                    }
                }
                break;
            default:
                DebugUtil.warnOut(TAG, "action = " + action);
                break;
        }
    }

    /*---------------------------getter & setter---------------------------*/

    /**
     * 设置获取到WiFi扫描的结果时的回调
     *
     * @param onWifiScanStateChangedListener 获取到WiFi扫描的结果时的回调
     */
    public void setOnWifiScanStateChangedListener(OnWifiScanStateChangedListener onWifiScanStateChangedListener) {
        this.wifiScanResultObtainedListener = onWifiScanStateChangedListener;
    }
}
